package kr.hkit.powerjava.chapter24.work2;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class StudentDao implements InterfaceDao<Student> {
	private ArrayList<Student> lists;
	private final static String FILE_NAME = "StudentList.data";

	@Override
	public void insertItem(Student item) {
		if (!lists.contains(item)) {
			lists.add(item);
		} else {
			System.out.println("중복된 데이터");
		}

	}

	public void deleteItem(int idx) {
		if(idx < lists.size()&& idx >=0){
			lists.remove(idx);
		}

	}

	@Override
	public void updateItem(int idx,Student item) {
		lists.set(index, element)

	}

	@Override
	public int searchItem(Student item) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	   public void prnItemList(Comparator<Student> comp) {
	      if ( comp == null){
	    	  Collections.sort(lists);
	      }else {
			Collections.sort(lists, comp);
		}
	      for(Student std: lists)
	    	  
	      
	   }

	@Override
	public void loadFile() {
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new BufferedInputStream(
					new FileInputStream(StudentDao.FILE_NAME)));
			lists = (ArrayList<Student>) ois.readObject();
		} catch (FileNotFoundException e) {
			try {
				new File(StudentDao.FILE_NAME).createNewFile();
				lists = new ArrayList<>();
			} catch (IOException e1) {
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ois != null)
				try {
					ois.close();
				} catch (Exception e) {
				}
		}

	}

	@Override
	public void saveFile() {
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new BufferedOutputStream(
					new FileOutputStream(StudentDao.FILE_NAME)));
			oos.writeObject(lists);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (oos != null)
				try {
					oos.close();
				} catch (Exception e) {
				}
		}

	}

		
}

